package sopt.issueChaser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import comm.util.SoptHashMap;

public class MetawebblogHelper {
	private XmlRpcClientConfigImpl xmlRpcConfig = new XmlRpcClientConfigImpl();
	private XmlRpcClient xmlRpcClient = new XmlRpcClient();
	
	public MetawebblogHelper(String server_url) throws MalformedURLException{
		xmlRpcConfig.setServerURL(new URL(server_url));
		xmlRpcClient.setConfig(xmlRpcConfig);
	}
	
	
	public ArrayList getCategoryList(String blogid,String userid,String userpassword){
		// 카테고리 목록 가져오기
		ArrayList lt = new ArrayList();
		
        Object[] params = new Object[] { blogid, userid, userpassword };

        Object[] returnedObj = null;
		try {
			returnedObj = (Object[]) xmlRpcClient.execute(
			        "metaWeblog.getCategories", params);
		} catch (XmlRpcException e) {
			return lt;
		}
        for (Object o : returnedObj) {
        	lt.add(genSoptHashMap((Map) o));
        }
        
		return lt;
	}
	
	
	public ArrayList getRecentPosts(String blogid,String userid,String userpassword,int length){
		// 최근 포스트 가져오기
		ArrayList lt = new ArrayList();
		Object[] params = new Object[] { blogid, userid, userpassword, length };

        Object[] returnedObj = null;
		try {
			returnedObj = (Object[]) xmlRpcClient.execute(
			        "metaWeblog.getRecentPosts", params);
		} catch (XmlRpcException e) {
			System.out.println(e.getMessage());
			return lt;
		}
        for (Object o : returnedObj) {
            lt.add(genSoptHashMap((Map) o));
        }
		return lt;
	}
	
	public String newPost(String blogid,String userid, String userpassword, String title, String desc, String category,String mt_keywords){
		// 포스팅
        Map<String, String> m = new HashMap<String, String>();
        
        if(title.length() < 1)
        	title = "[제목없음]";
        if(desc.length()<1)
        	desc = "[내용 없음]";
        System.out.println("내용 넘어 오질않는감?!!?!?!?!??");
        System.out.println(desc);
        desc+="<div style='padding-top:10px;text-align:right;'><a href='http://issuechaser.cafe24.com/'>본 글은 이슈 체이서로 작성되었습니다." +
        		(new Date()).toLocaleString()+
        		"</a></div>";
        //글 제목
        m.put("title", title);
                
        //본문
        m.put("description", desc);

        //카테고리 설정 (넘어온 리스트에 있는 글자로 설정);
        if(category.length()>0)
        	m.put("categories", category);
        
        //,로 구분 되는 태그
        m.put("mt_keywords", mt_keywords);
        
        Object[] params = new Object[]{blogid, userid, userpassword, m, true};
        
        String result = "0";
		try {
			result = xmlRpcClient.execute(
			        "metaWeblog.newPost", params).toString();
		} catch (XmlRpcException e) {
			return result;
		}
//        이글루스는 저장한 글번호 리턴
        return result;
	}
	
	
	
	private SoptHashMap genSoptHashMap(Map map){
		SoptHashMap hm = new SoptHashMap();
        for (Object key : map.keySet()) {
        	hm.put(key, map.get(key));
        }
        return hm;
	}
    

    
    
}
